<?php

namespace App\Http\Controllers\Blog;

use App\Events\ModelView;
use App\Models\Category;
use App\Http\Controllers\Controller;
use App\Models\Article;

class ArticleController extends Controller
{
    protected $article;

    public function __construct(Article $article)
    {
        $this->article = $article->publish();
    }
    
    public function index()
    {
        $article_list = $this->article->orderBy('created_at', 'desc')
            ->select('id', 'subtitle', 'title', 'created_at', 'content', 'copyfrom')
            ->paginate();
        return view('blog.index', compact('article_list'));
    }


    public function show($id = 0, $slug = '')
    {
        if ($id) {
            $article = $this->article->find($id);
        } elseif($slug) {
            $article = $this->article->where('subtitle', $slug)->first();
        } else {
            abort(404, 'Contents not found!');
        }

        if (!$this->checkArticlePassword($article)) {
            return back()->withErrors(['article_password' => '密码错误！']);
        }

        if (empty($article)) {
            return abort(404, 'Contents not found!');
        } else{
            // 统计访问次数
            event(new ModelView($article, 'article'));

            $next = Article::getNextArticle($article->id);
            $previous = Article::getPreviousArticle($article->id);
            return view('blog.show', compact('article', 'next', 'previous'));
        }
    }

    public function category($cid, Category $category)
    {
        $categoryInfo = $category->where('id', $cid)->first();
        if (empty($categoryInfo)) {
            abort(404, 'Contents not found!');
        }
        $title = $categoryInfo->name;
        if ($categoryInfo->pid == 0) {
            $article_list = $this->article->where('top_cid', $cid)
                ->orderBy('created_at', 'desc')
                ->select('id', 'subtitle', 'title', 'created_at', 'content', 'copyfrom')
                ->paginate();
        } else {
            $article_list = $this->article->where('cid', $cid)
                ->orderBy('created_at', 'desc')
                ->select('id', 'subtitle', 'title', 'created_at', 'content')
                ->paginate();
        }
        return view('blog.category', compact('article_list', 'title'));
    }

    public function archives()
    {
        $archives = Article::getArticleByYear();
        return view('blog.archives', compact('archives'));
    }

    public function search()
    {
        $q = request()->post('q');
        $article_list = $this->article->orderBy('created_at', 'desc')
            ->where('title', 'like', '%' . $q . '%')
            ->select('id', 'subtitle', 'title', 'created_at', 'content')
            ->paginate();
        return view('blog.index', compact('article_list', 'q'));
    }

    /**
     * 检验文章密码
     * @param $article
     * @return bool
     */
    protected function checkArticlePassword($article)
    {
        if (empty($article->password)) {
            return true;
        }

        if (request()->isMethod('POST') && !hash_equals($article->password, request()->post('article_password'))) {
            return false;
        }

        return true;
    }

}
